<?php


namespace app\super\controller;


use think\facade\Config;
use think\Db;

class User extends \Common {

    #获取代拍公司名下的送、买人员列表
    public function GetUsersBySub(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_GET["page"]) && isset($_GET["limit"]) && isset($_GET["type"])){
            if(isset($_GET["subid"])){
                $aid = $_GET["subid"];
            }
            $list = []; $count = 0; $page = $_GET["page"]; $limit = $_GET["limit"];
            if($_GET["type"] === "send"){
                if($_GET["search_key"] == "idcard" && isset($_GET["search_val"]) && $_GET["search_val"] != ""){
                    $user_send = Db::name("user_send_company")->alias("a")
                        ->join("c_user_send b", "a.us_id = b.us_id")
                        ->where("a.aid", $aid)
                        ->where("b.idcard", $_GET["search_val"])
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_send_company")->alias("a")
                        ->join("c_user_send b", "a.us_id = b.us_id")
                        ->where("a.aid", $aid)
                        ->where("b.idcard", $_GET["search_val"])
                        ->count();
                    if(empty($user_send)){
                        $user_send = Db::name("user_send")
                            ->where("idcard", $_GET["search_val"])
                            ->page($page, $limit)
                            ->select();
                        $count = Db::name("user_send")
                            ->where("idcard", $_GET["search_val"])
                            ->count();
                    }
                }else if($_GET["search_key"] == "tel" && isset($_GET["search_val"]) && $_GET["search_val"] != ""){
                    $user_send = Db::name("user_send_company")->alias("a")
                        ->join("c_user_send b", "a.us_id = b.us_id")
                        ->where("a.aid", $aid)
                        ->where(function ($query){
                            $query->where("b.tel1", $_GET["search_val"])->whereOr("b.tel2", $_GET["search_val"]);
                        })
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_send_company")->alias("a")
                        ->join("c_user_send b", "a.us_id = b.us_id")
                        ->where("a.aid", $aid)
                        ->where(function ($query){
                            $query->where("b.tel1", $_GET["search_val"])->whereOr("b.tel2", $_GET["search_val"]);
                        })
                        ->count();
                    if(empty($user_send)){
                        $user_send = Db::name("user_send")
                            ->where("tel1", $_GET["search_val"])
                            ->whereOr("tel2", $_GET["search_val"])
                            ->page($page, $limit)
                            ->select();
                        $count = Db::name("user_send")
                            ->where("tel1", $_GET["search_val"])
                            ->whereOr("tel2", $_GET["search_val"])
                            ->count();
                    }
                }else{
                    $user_send = Db::name("user_send_company")
                        ->where("aid", $aid)
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_send_company")
                        ->where("aid", $aid)
                        ->count();
                }
                foreach ($user_send as $value){
                    $baseInfo = Db::name("user_send")->where("us_id", $value["us_id"])->find();
                    $list[] = [
                        "base" => [
                            "id" => (int)$baseInfo["us_id"],
                            "idcard" => $baseInfo["idcard"],
                            "name" => $baseInfo["name"],
                            "tel1" => $baseInfo["tel1"],
                            "tel2" => $baseInfo["tel2"],
                            "country" => $baseInfo["country"],
                            "email" => $baseInfo["email"],
                            "line" => $baseInfo["line"],
                            "wxid" => $baseInfo["wxid"],
                            "address" => $baseInfo["address"],
                            "postcode" => $baseInfo["postcode"],
                            "job" => $baseInfo["job"],
                            "photo" => $baseInfo["photo"] == "" ? "" : Config::get("app.ip")."static/upload/people/photo/send/{$baseInfo["us_id"]}/".$baseInfo["photo"]."?t=".time(),
                            "idcardPhoto1" => $baseInfo["idcardPhoto1"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/send/{$baseInfo["us_id"]}/".$baseInfo["idcardPhoto1"]."?t=".time(),
                            "idcardPhoto2" => $baseInfo["idcardPhoto2"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/send/{$baseInfo["us_id"]}/".$baseInfo["idcardPhoto2"]."?t=".time(),
                            "passport" => $baseInfo["passport"] == "" ? "" : Config::get("app.ip")."static/upload/people/passport/send/{$baseInfo["us_id"]}/".$baseInfo["passport"]."?t=".time(),
                            "bankcard" => $baseInfo["bankcard"],
                            "bankname" => $baseInfo["bankname"],
                        ],
                        "send" => !isset($value["usc_id"]) ? [
                            "catafee" => 0,
                            "id" => "null-".$value["us_id"]."-".$aid,
                            "subid" => "null-".$value["us_id"]."-".$aid,
                            "number" => "",
                            "deal_level" => 0,
                            "deal_point" => 0,
                            "send_level" => 0,
                            "fee_rate" => 0,
                            "remark" => "",
                        ] : [
                            "catafee" => $value["catafee"],
                            "id" => $value["usc_id"],
                            "subid" => (int)$value["aid"],
                            "number" => $value["number"],
                            "deal_level" => $value["deal_level"] == "" ? 0 : (int)$value["deal_level"],
                            "deal_point" => $value["deal_point"] == "" ? 0 : (int)$value["deal_point"],
                            "send_level" => $value["send_level"] == "" ? 0 : (int)$value["send_level"],
                            "fee_rate" => $value["fee_rate"] * 10000 / 1000000,
                            "remark" => $value["remark"],
                        ]
                    ];
                }
            }else if($_GET["type"] === "buy"){
                if($_GET["search_key"] == "idcard" && isset($_GET["search_val"]) && $_GET["search_val"] != ""){
                    $user_buy = Db::name("user_buy_company")->alias("a")
                        ->join("c_user_buy b", "a.ub_id = b.ub_id")
                        ->where("b.idcard", $_GET["search_val"])
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_buy_company")->alias("a")
                        ->join("c_user_buy b", "a.ub_id = b.ub_id")
                        ->where("b.idcard", $_GET["search_val"])
                        ->count();
                    if(empty($user_buy)){
                        $user_buy = Db::name("user_buy")
                            ->where("idcard", $_GET["search_val"])
                            ->page($page, $limit)
                            ->select();
                        $count = Db::name("user_buy")
                            ->where("idcard", $_GET["search_val"])
                            ->count();
                    }
                }else if($_GET["search_key"] == "num" && isset($_GET["search_val"]) && $_GET["search_val"] != ""){
                    $user_buy = Db::name("user_buy_company")->alias("a")
                        ->join("c_num b", "(a.ub_id = b.ub_id) and (a.aid = b.aid)")
                        ->where("a.aid", $aid)
                        ->where("b.number", $_GET["search_val"])
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_buy_company")->alias("a")
                        ->join("c_num b", "(a.ub_id = b.ub_id) and (a.aid = b.aid)")
                        ->where("a.aid", $aid)
                        ->where("b.number", $_GET["search_val"])
                        ->count();
                }else if($_GET["search_key"] == "tel" && isset($_GET["search_val"]) && $_GET["search_val"] != ""){
                    $user_buy = Db::name("user_buy_company")->alias("a")
                        ->join("c_user_buy b", "a.ub_id = b.ub_id")
                        ->where("a.aid", $aid)
                        ->where(function ($query){
                            $query->where("b.tel1", $_GET["search_val"])->whereOr("b.tel2", $_GET["search_val"]);
                        })
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_buy_company")->alias("a")
                        ->join("c_user_buy b", "a.ub_id = b.ub_id")
                        ->where("a.aid", $aid)
                        ->where(function ($query){
                            $query->where("b.tel1", $_GET["search_val"])->whereOr("b.tel2", $_GET["search_val"]);
                        })
                        ->count();
                    if(empty($user_buy)){
                        $user_buy = Db::name("user_buy")
                            ->where("tel1", $_GET["search_val"])
                            ->whereOr("tel2", $_GET["search_val"])
                            ->page($page, $limit)
                            ->select();
                        $count = Db::name("user_buy")
                            ->where("tel1", $_GET["search_val"])
                            ->whereOr("tel2", $_GET["search_val"])
                            ->count();
                    }
                }else{
                    $user_buy = Db::name("user_buy_company")
                        ->where("aid", $aid)
                        ->page($page, $limit)
                        ->select();
                    $count = Db::name("user_buy_company")
                        ->where("aid", $aid)
                        ->count();
                }
                foreach ($user_buy as $value){
                    $baseInfo = Db::name("user_buy")->where("ub_id", $value["ub_id"])->find();
                    $number = Db::name("num")->where("ub_id", $value["ub_id"])->where("aid", $aid)->find();
                    $num = "";
                    if(!empty($number)){
                        $num = $number["number"];
                    }
                    $list[] = [
                        "base" => [
                            "id" => (int)$baseInfo["ub_id"],
                            "idcard" => $baseInfo["idcard"],
                            "name" => $baseInfo["name"],
                            "tel1" => $baseInfo["tel1"],
                            "tel2" => $baseInfo["tel2"],
                            "country" => $baseInfo["country"],
                            "email" => $baseInfo["email"],
                            "line" => $baseInfo["line"],
                            "wxid" => $baseInfo["wxid"],
                            "address" => $baseInfo["address"],
                            "postcode" => $baseInfo["postcode"],
                            "job" => $baseInfo["job"],
                            "photo" => $baseInfo["photo"] == "" ? "" : Config::get("app.ip")."static/upload/people/photo/buy/{$baseInfo["ub_id"]}/".$baseInfo["photo"]."?t=".time(),
                            "idcardPhoto1" => $baseInfo["idcardPhoto1"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/buy/{$baseInfo["ub_id"]}/".$baseInfo["idcardPhoto1"]."?t=".time(),
                            "idcardPhoto2" => $baseInfo["idcardPhoto2"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/buy/{$baseInfo["ub_id"]}/".$baseInfo["idcardPhoto2"]."?t=".time(),
                            "passport" => $baseInfo["passport"] == "" ? "" : Config::get("app.ip")."static/upload/people/passport/buy/{$baseInfo["ub_id"]}/".$baseInfo["passport"]."?t=".time(),
                            "bankcard" => $baseInfo["bankcard"],
                            "bankname" => $baseInfo["bankname"],
                        ],
                        "buy" => !isset($value["ubc_id"]) ? [
                            "id" => "null-".$baseInfo["ub_id"]."-".$aid,
                            "subid" => "null-".$baseInfo["ub_id"]."-".$aid,
                            "buy_level" => 0,
                            "credit_level" => 0,
                            "judg_level" => 0,
                            "fee_rate" => 0,
                            "money" => 0,
                            "mstate" => 0,
                            "num" => "",
                            "remark" => ""
                        ] : [
                            "id" => (int)$value["ubc_id"],
                            "subid" => (int)$value["aid"],
                            "buy_level" => $value["buy_level"] == "" ? 0 : (int)$value["buy_level"],
                            "credit_level" => $value["credit_level"] == "" ? 0 : (int)$value["credit_level"],
                            "judg_level" => $value["judg_level"] == "" ? 0 : (int)$value["judg_level"],
                            "fee_rate" => (float)$value["fee_rate"] * 10000 / 1000000,
                            "money" => (float)$value["money"],
                            "mstate" => $value["pay_state"] > 0 ? 1 : 0,
                            "num" => $num,
                            "remark" => $value["remark"]
                        ]
                    ];
                }
            }else{
                Code(403, "服务器错误，请稍后再试");
            }
            $arr = [
                "list" => $list,
                "total" => $count
            ];
            Code(200, false, $arr);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    public function GetUsers(){}

    # 新增用户到公司旗下
    private function AddUserToCom($uid, $aid, $type = "buy"){
        if($type === "buy"){
            $check = Db::name("user_buy_company")->where("ub_id", $uid)->where("aid", $aid)->find();
            if(empty($check)){
                $insert = [
                    "ub_id" => $uid,
                    "aid" => $aid,
                    "reg_time" => time(),
                ];
                return Db::name("user_buy_company")->insertGetId($insert);
            }else{
                return $check["ubc_id"];
            }
        }else{
            $check = Db::name("user_send_company")->where("us_id", $uid)->where("aid", $aid)->find();
            if(empty($check)){
                $insert = [
                    "us_id" => $uid,
                    "aid" => $aid,
                    "reg_time" => time(),
                ];
                return Db::name("user_send_company")->insertGetId($insert);
            }else{
                return $check["usc_id"];
            }
        }
    }

    # 修改用户信息
    public function UpdateUser(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        $msg = "";
        # 查看身份证号码是否存在基本表
        if((isset($_POST["buy"]) || isset($_POST["send"])) && isset($_POST["base"])){
            $base = json_decode($_POST["base"], true);
            if(isset($_POST["buy"])){
                $bind = false;
                $buy = json_decode($_POST["buy"], true);
                if(isset($buy["subid"]) && is_numeric($buy["subid"])){
                    $aid = $buy["subid"];
                }
                if($buy["key"] != ""){
                    $number = Db::name("num")
                        ->where("aid", $aid)
                        ->where("number", $buy["num"])
                        ->where("password", $buy["key"])
                        ->find();
                    if(!empty($number)){
                        if($number["ub_id"] != 0){
                            Code(403, "号牌已经被绑定，请核对后再试");
                        }
                        $bind = true;
                    }else{
                        Code(403, "号牌或者密码错误，请核对");
                    }
                }

                $arr = ["name", "idcard", "tel1", "tel2", "country", "email", "line", "wxid", "address", "postcode", "job", "bankcard", "bankname"];
                foreach ($arr as $value){
                    if(!isset($base[$value])){
                        $base[$value] = "";
                    }
                }

                $info = [
                    "name" => $base["name"],
                    "idcard" => $base["idcard"],
                    "tel1" => $base["tel1"],
                    "tel2" => $base["tel2" ],
                    "country" => $base["country"],
                    "email" => $base["email"],
                    "line" => $base["line"],
                    "wxid" => $base["wxid"],
                    "address" => $base["address"],
                    "postcode" => $base["postcode"],
                    "job" => $base["job"],
                    "bankcard" => $base["bankcard"],
                    "bankname" => $base["bankname"],
                ];

                # 添加
                if(!isset($base["id"])){
                    # 检查手机号码和姓名是否存在
                    if($base["tel2"] != ""){
                        $check = Db::name("user_buy")
                            ->where(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel1", $base["tel2"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel2"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"])->where("name", $base["name"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel2"])->where("name", $base["name"]);
                            })
                            ->select();
                    }else{
                        $check = Db::name("user_buy")
                            ->where(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel1"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"])->where("name", $base["name"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel1"])->where("name", $base["name"]);
                            })
                            ->select();
                    }
                    if(empty($check)){
                        $ub_id = Db::name("user_buy")->insertGetId($info);
                        $up = [
                            "nickname" => "User_".$ub_id
                        ];
                        Db::name("user_buy")->where("ub_id", $ub_id)->update($up);
                        $add = [
                            "ub_id" => $ub_id,
                            "aid" => $aid,
                            "reg_time" => time(),
                            "buy_level" => $buy["buy_level"],
                            "credit_level" => $buy["credit_level"],
                            "judg_level" => $buy["judg_level"],
                            "fee_rate" => $buy["fee_rate"] == "" ? 0 : (float)$buy["fee_rate"] * 100,
                            "money" => $buy["money"] == "" ? 0 : $buy["money"],
                            "pay_state" => ($buy["money"] != "" && $buy["money"] != 0) ? 1 : 0,
                            "remark" => $buy["remark"]
                        ];
                        if($buy["money"] != "" && $buy["money"] != 0){
                            $this->RecordMoney($ub_id, $this->token["id"], 0, $buy["money"], $buy["remark"] , 1);
                        }
                        Db::name("user_buy_company")->insertGetId($add);
                        $msg = "用户添加成功";
                    }else{
                        if($check[0]["name"] == $base["name"]){
                            Code(403, "用户已存在，为避免数据混乱，请前去搜索修改");
                        }else{
                            Code(403, "手机号已被注册，请核对");
                        }
                    }
                }else{ # 修改
                    # 检查手机号码和姓名是否存在
                    if($base["tel2"] != ""){
                        $check = Db::name("user_buy")
                            ->where("ub_id != {$base["id"]}")
                            ->where(function ($query) use ($base){
                                $query
                                    ->where(function ($query1) use ($base){
                                        $query1->where("tel1", $base["tel1"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel1", $base["tel2"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel2", $base["tel2"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel1", $base["tel1"])->where("name", $base["name"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel2", $base["tel2"])->where("name", $base["name"]);
                                    });
                            })
                            ->select();
                    }else{
                        $check = Db::name("user_buy")
                            ->where("ub_id != {$base["id"]}")
                            ->where(function ($query) use ($base){
                                $query
                                    ->where(function ($query) use ($base){
                                        $query->where("tel1", $base["tel1"]);
                                    })
                                    ->whereOr(function ($query) use ($base){
                                        $query->where("tel2", $base["tel1"]);
                                    })
                                    ->whereOr(function ($query) use ($base){
                                        $query->where("tel1", $base["tel1"])->where("name", $base["name"]);
                                    })
                                    ->whereOr(function ($query) use ($base){
                                        $query->where("tel2", $base["tel1"])->where("name", $base["name"]);
                                    });
                            })
                            ->select();
                    }
                    if(empty($check)){
                        $ub_id = $base["id"];
                        Db::name("user_buy")->where("ub_id", $ub_id)->update($info);
                        if(!is_numeric($buy["subid"])){
                            $str = explode("-", $buy["subid"]);
                            $aid = $str[2];
                            $ubc_id = $this->AddUserToCom($str[1], $aid);
                            if(!is_numeric($ubc_id)){
                                Code(403, "服务器错误，请稍后再试");
                            }
                            $buy["id"] = $ubc_id; $buy["subid"] = $str[1];
                        }
                        $before = Db::name("user_buy_company")->where("ubc_id", $buy["id"])->find();
                        $update = [
                            "ub_id" => $ub_id,
                            "aid" => $aid,
                            "reg_time" => time(),
                            "buy_level" => $buy["buy_level"],
                            "credit_level" => $buy["credit_level"],
                            "judg_level" => $buy["judg_level"],
                            "fee_rate" => $buy["fee_rate"] == "" ? 0 : (float)$buy["fee_rate"] * 100,
                            "money" => $buy["money"] == "" ? 0 : $buy["money"],
                            "pay_state" => ($buy["money"] != "" && $buy["money"] != 0) ? 1 : $buy["mstate"],
                            "remark" => $buy["remark"]
                        ];
                        if($before["money"] != $buy["money"]){
                            $state = -1;
                            if($before["money"] == 0){
                                $state = 1;
                            }
                            $this->RecordMoney($ub_id, $this->token["id"], $before["money"], $buy["money"], $buy["remark"] , $state);
                        }
                        Db::name("user_buy_company")->where("ubc_id", $buy["id"])->update($update);
                        $msg = "用户信息修改成功";
                    }else{
                        Code(403, "手机号码已经被注册，请核对");
                    }
                }
                $update = [];
                if(isset($_FILES["idcardPhoto1"])){
                    $update["idcardPhoto1"] = UploadFile($_FILES["idcardPhoto1"], $ub_id."_idcard1", "./static/upload/people/idcard/buy/$ub_id/", true);
                }
                if(isset($_FILES["idcardPhoto2"])){
                    $update["idcardPhoto2"] = UploadFile($_FILES["idcardPhoto2"], $ub_id."_idcard2", "./static/upload/people/idcard/buy/$ub_id/", true);
                }
                if(isset($_FILES["photo"])){
                    $update["photo"] = UploadFile($_FILES["photo"], $ub_id."_photo", "./static/upload/people/photo/buy/$ub_id/", true);
                }
                if(isset($_FILES["passport"])){
                    $update["passport"] = UploadFile($_FILES["passport"], $ub_id."_passport", "./static/upload/people/passport/buy/$ub_id/", true);
                }
                if(!empty($update)){
                    Db::name("user_buy")->where("ub_id", $ub_id)->update($update);
                }

                if($bind){
                    $bindnum = [
                        "ub_id" => $ub_id
                    ];
                    Db::name("num")
                        ->where("aid", $aid)
                        ->where("number", $buy["num"])
                        ->update($bindnum);
                }
                Code(200, $msg, (Object)[]);
            }else if(isset($_POST["send"])){
                $send = json_decode($_POST["send"], true);
                if(isset($buy["subid"]) && is_numeric($send["subid"])){
                    $aid = $send["subid"];
                }
                $info = [
                    "name" => $base["name"],
                    "idcard" => $base["idcard"],
                    "tel1" => $base["tel1"],
                    "tel2" => $base["tel2" ],
                    "country" => $base["country"],
                    "email" => $base["email"],
                    "line" => $base["line"],
                    "wxid" => $base["wxid"],
                    "address" => $base["address"],
                    "postcode" => $base["postcode"],
                    "job" => $base["job"],
                    "bankcard" => $base["bankcard"],
                    "bankname" => $base["bankname"],
                ];

                # 添加
                if(!isset($base["id"])){
                    # 检查手机号码和姓名是否存在
                    if($base["tel2"] != ""){
                        $check = Db::name("user_send")
                            ->where(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel1", $base["tel2"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel2"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"])->where("name", $base["name"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel2"])->where("name", $base["name"]);
                            })
                            ->select();
                    }else{
                        $check = Db::name("user_send")
                            ->where(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel1"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel1", $base["tel1"])->where("name", $base["name"]);
                            })
                            ->whereOr(function ($query) use ($base){
                                $query->where("tel2", $base["tel1"])->where("name", $base["name"]);
                            })
                            ->select();
                    }
                    if(empty($check)){
                        $us_id = Db::name("user_send")->insertGetId($info);
                        $add = [
                            "us_id" => $us_id,
                            "aid" => $aid,
                            "reg_time" => time(),
                            "send_level" => $send["send_level"],
                            "deal_level" => $send["deal_level"],
                            "deal_point" => $send["deal_point"],
                            "fee_rate" => $send["fee_rate"] == "" ? 0 : (float)$send["fee_rate"] * 100,
                            "remark" => $send["remark"]
                        ];
                        Db::name("user_send_company")->insertGetId($add);
                        $msg = "用户添加成功";
                    }else{
                        if($check[0]["name"] == $base["name"]){
                            Code(403, "用户已存在，为避免数据混乱，请前去搜索修改");
                        }else{
                            Code(403, "手机号已被注册，请核对");
                        }
                    }
                }else{ # 修改
                    # 检查手机号码和姓名是否存在
                    if($base["tel2"] != ""){
                        $check = Db::name("user_send")
                            ->where("us_id != {$base["id"]}")
                            ->where(function ($query) use ($base){
                                $query
                                    ->where(function ($query1) use ($base){
                                        $query1->where("tel1", $base["tel1"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel1", $base["tel2"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel2", $base["tel2"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel1", $base["tel1"])->where("name", $base["name"]);
                                    })
                                    ->whereOr(function ($query1) use ($base){
                                        $query1->where("tel2", $base["tel2"])->where("name", $base["name"]);
                                    });
                            })
                            ->select();
                    }else{
                        $check = Db::name("user_send")
                            ->where("us_id != {$base["id"]}")
                            ->where(function ($query) use ($base){
                                $query
                                    ->where(function ($query) use ($base){
                                        $query->where("tel1", $base["tel1"]);
                                    })
                                    ->whereOr(function ($query) use ($base){
                                        $query->where("tel2", $base["tel1"]);
                                    })
                                    ->whereOr(function ($query) use ($base){
                                        $query->where("tel1", $base["tel1"])->where("name", $base["name"]);
                                    })
                                    ->whereOr(function ($query) use ($base){
                                        $query->where("tel2", $base["tel1"])->where("name", $base["name"]);
                                    });
                            })
                            ->select();
                    }
                    if(empty($check)){
                        $us_id = $base["id"];
                        Db::name("user_send")->where("us_id", $us_id)->update($info);
                        if(!is_numeric($send["subid"])){
                            $str = explode("-", $send["subid"]);
                            $aid = $str[2];
                            $ubc_id = $this->AddUserToCom($str[1], $aid, "send");
                            if(!is_numeric($ubc_id)){
                                Code(403, "服务器错误，请稍后再试");
                            }
                            $send["id"] = $ubc_id; $send["subid"] = $str[1];
                        }

                        $update = [
                            "us_id" => $us_id,
                            "aid" => $aid,
                            "reg_time" => time(),
                            "number" => $send["number"],
                            "send_level" => $send["send_level"],
                            "deal_level" => $send["deal_level"],
                            "deal_point" => $send["deal_point"],
                            "fee_rate" => $send["fee_rate"] == "" ? 0 : (float)$send["fee_rate"] * 100,
                            "remark" => $send["remark"]
                        ];
                        Db::name("user_send_company")->where("usc_id", $send["id"])->update($update);
                        $msg = "用户信息修改成功";
                    }else{
                        Code(403, "手机号码已经被注册，请核对");
                    }
                }
                $update = [];
                if(isset($_FILES["idcardPhoto1"])){
                    $update["idcardPhoto1"] = UploadFile($_FILES["idcardPhoto1"], $us_id."_idcard1", "./static/upload/people/idcard/send/$us_id/", true);
                }
                if(isset($_FILES["idcardPhoto2"])){
                    $update["idcardPhoto2"] = UploadFile($_FILES["idcardPhoto2"], $us_id."_idcard2", "./static/upload/people/idcard/send/$us_id/", true);
                }
                if(isset($_FILES["photo"])){
                    $update["photo"] = UploadFile($_FILES["photo"], $us_id."_photo", "./static/upload/people/photo/send/$us_id/", true);
                }
                if(isset($_FILES["passport"])){
                    $update["passport"] = UploadFile($_FILES["passport"], $us_id."_passport", "./static/upload/people/passport/send/$us_id/", true);
                }
                if(!empty($update)){
                    Db::name("user_send")->where("us_id", $us_id)->update($update);
                }

                Code(200, $msg, (Object)[]);
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 设置VIP
    public function SetVipState(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_POST["id"]) && isset($_POST["mstate"])){
            $id = $_POST["id"]; $mstate = $_POST["mstate"];
            if(!is_numeric($id)){
                $str = explode("-", $id);
                $aid = $str[2];
                $id = $this->AddUserToCom($str[1], $aid);
                if(!is_numeric($id)){
                    Code(403, "服务器错误，请稍后再试");
                }
            }
            $update = [
                "pay_state" => $mstate
            ];
            Db::name("user_buy_company")->where("ubc_id", $id)->update($update);
            Code(200, "会员状态更新成功", (Object)[]);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 修改备注
    public function SetRemark(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_POST["id"]) && isset($_POST["remark"]) && isset($_POST["type"])){
            $remark = $_POST["remark"]; $id = $_POST["id"]; $type = $_POST["type"];
            if(!is_numeric($id)){
                $str = explode("-", $id);
                $aid = $str[2];
                $id = $this->AddUserToCom($str[1], $aid, $type);
                if(!is_numeric($id)){
                    Code(403, "服务器错误，请稍后再试");
                }
            }
            $update = [
                "remark" => $remark
            ];
            switch ($_POST["type"]){
                case "buy" : Db::name("user_buy_company")->where("ubc_id", $id)->update($update); break;
                case "send" : Db::name("user_send_company")->where("usc_id", $id)->update($update); break;
            }
            Code(200, "修改成功");
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }
}